using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Pokemon
{
    class Lightning: Bullet
    {
        protected static List<Texture2D> _texture2Ds = new List<Texture2D>();

        protected static List<Vector2> _sizeTextures = new List<Vector2>();

        protected static List<int> _numFrame = new List<int>();

        protected static int _speed;

        protected static string _pathXML;//Chua duong dan den file dac ta XML

        public Lightning(Game game, Unit owner, Unit target) 
            : base(game, owner, target)
        {

        }

        public new static void Initialize()
        {
            XDocument doc = XDocument.Load(_pathXML);
            XName name = XName.Get(@"Speed");            
            if (doc.Document != null)
            {
                var definitions = doc.Document.Descendants(name);
                _speed = Int32.Parse((definitions.First()).Value);

                name = XName.Get(@"Style");
                definitions = doc.Document.Descendants(name);
                foreach (var definition in definitions)
                {
                    var xAttribute = definition.Attribute(@"SheetName");
                    if (xAttribute != null)
                    {
                        _texture2Ds.Add(_game.Content.Load<Texture2D>(xAttribute.Value));
                        xAttribute = definition.Attribute(@"NumFrame");
                        if (xAttribute != null)
                        {
                            var value = Int32.Parse(xAttribute.Value);
                            _numFrame.Add(value);
                        }

                        xAttribute = definition.Attribute(@"FrameWidth");
                        if (xAttribute != null)
                        {
                            var width = Int32.Parse(xAttribute.Value);
                            xAttribute = definition.Attribute(@"FrameHeight");
                            if (xAttribute != null)
                            {
                                var height = Int32.Parse(xAttribute.Value);
                                _sizeTextures.Add(new Vector2(width, height));
                            }
                        }
                    }
                }
            }
        }

        public static void AddPath(string path)
        {
            _pathXML = path;
        }

        public override void Update(GameTime gameTime)
        {
            try
            {
                _timeSpan += gameTime.ElapsedGameTime;
                if (_timeSpan.Milliseconds < 300)
                    return;
                base.Update(gameTime);
                _timeSpan = TimeSpan.Zero;
                NextFrame(); 
            }
            catch (Exception)
            {                
                throw;
            }
              
        }

        public void NextFrame()
        {
            _frameIndex = (_frameIndex + 1) % (_numFrame[0]);//Vi 3 hinh co so frame bang nhau
        }

        public override void Draw(GameTime gameTime)
        {
            _timeSpan += gameTime.ElapsedGameTime;
            //if (_timeSpan.Milliseconds < 100)
            //    return;
            //_timeSpan = TimeSpan.Zero;
            try
            {
                _SpriteBatch.Draw(_texture2Ds[0], new Vector2(_owner.Position.X + _owner.Textures[0].CenterPoint.X - _sizeTextures[0].X / 2,
                                                            0/*_owner.Position.Y - _sizeTextures[0].Y*/),
                                                            GetNextRect(0), Color.White, 0.0f, Vector2.Zero,
                                                            new Vector2(1, _owner.Position.Y / _sizeTextures[0].Y),
                                                            SpriteEffects.None, 0);

                _SpriteBatch.Draw(_texture2Ds[1], new Vector2(_target.GetCenterPoint().X - _sizeTextures[1].X / 2,
                                                                _target.Owner.Position.Y + _target.Owner.Size.Y - _sizeTextures[0].Y / _numFrame[0] - 25),
                                                                GetNextRect(1), Color.White);

                Vector2 centerPoint = new Vector2(_target.GetCenterPoint().X, _target.GetCenterPoint().Y);

                _SpriteBatch.Draw(_texture2Ds[2], new Vector2(centerPoint.X - _sizeTextures[2].X / (_numFrame[2] * 2), 0/*centerPoint.Y - _sizeTextures[2].Y / 2*/),
                                                                GetNextRect(2), Color.White, 0.0f, Vector2.Zero,
                                                                new Vector2(1, _owner.Position.Y / _sizeTextures[2].Y),
                                                                SpriteEffects.None, 0);

                if (_frameIndex == _numFrame[0] - 1)
                {
                    if (_target.Owner != null)
                        _target.TakeDamage(_owner.Damage, Color.Yellow);

                    BulletManager b = (BulletManager)Game.Services.GetService(typeof(BulletManager));
                    b.Remove(this);
                    ((Pikachu)_owner).RemoveEvent();
                }
            }            
            catch(Exception)
            {
                
            }
        }
        
        public Rectangle GetNextRect(int id)
        {
            Rectangle rect = new Rectangle();
            if (id == 1)
            {
                rect = new Rectangle(0, _frameIndex * (int)_sizeTextures[id].Y / _numFrame[id], (int)_sizeTextures[id].X, (int)_sizeTextures[id].Y / _numFrame[id]);
                return rect;
            }
            rect = new Rectangle(_frameIndex * (int)_sizeTextures[id].X / _numFrame[id], 0, (int)_sizeTextures[id].X / _numFrame[id], (int)_sizeTextures[id].Y);
            return rect;
        }
    }
}
